package com.wanpinghui.supplier.user.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wanpinghui.supplier.R;
import com.wanpinghui.wphlibrary.model.user.bean.AreaBean;

import java.util.List;

/**
 * 功能描述
 * author:caitiangui
 * time:2017/12/6
 */

public class SelectCityAdapter extends BaseExpandableListAdapter {


    private final List<AreaBean> areaBeanList;
    private Context context;

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    public AreaBean getData(int parentPosition, int childPosition) {
        if (getGroupCount() > parentPosition) {
            AreaBean areaBean = areaBeanList.get(parentPosition);
            if (areaBean.getChildren() != null && areaBean.getChildren().size() > childPosition) {
                return areaBean.getChildren().get(childPosition);
            }
        }
        return null;
    }

    public interface OnItemClickListener {
        void onItemClick(int groupPosition, int childPosition);

        void onItemGroupClick(int groupPosition);
    }

    public SelectCityAdapter(List<AreaBean> areaBeanList, Context context) {
        this.areaBeanList = areaBeanList;
        this.context = context;
    }

    @Override
    public int getGroupCount() {
        return areaBeanList == null ? 0 : areaBeanList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (areaBeanList.get(groupPosition).getChildren() != null) {
            return areaBeanList.get(groupPosition).getChildren().size();
        }
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return areaBeanList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return areaBeanList.get(groupPosition).getChildren();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        final GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_area, parent, false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.textViewContent = (TextView) convertView.findViewById(R.id.tv_content);
            groupViewHolder.imageViewSelect = (ImageView) convertView.findViewById(R.id.iv_select);
            groupViewHolder.ll_area = (RelativeLayout) convertView.findViewById(R.id.ll_area);
            groupViewHolder.iv_jiantou = (ImageView) convertView.findViewById(R.id.iv_jiantou);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        groupViewHolder.imageViewSelect.setVisibility(View.GONE);
        groupViewHolder.ll_area.setEnabled(false);
        final AreaBean areaBeen = areaBeanList.get(groupPosition);
        if (areaBeen.getChildren() == null || areaBeen.getChildren().size() <= 0) {
            groupViewHolder.iv_jiantou.setVisibility(View.INVISIBLE);

        } else {
            groupViewHolder.iv_jiantou.setVisibility(View.VISIBLE);
            if (isExpanded) {
                groupViewHolder.iv_jiantou.animate().setDuration(500).rotation(180).start();
//                groupViewHolder.imageViewSelect.setSelected(true);
            } else {
                groupViewHolder.iv_jiantou.animate().setDuration(500).rotation(0).start();
//                groupViewHolder.imageViewSelect.setSelected(false);
            }
        }
        groupViewHolder.imageViewSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //  if (onItemClickListener != null) {
                if (areaBeen.isSelect()) {
                    areaBeen.setSelect(false);
                    groupViewHolder.imageViewSelect.setSelected(false);
                    //           setSelect(groupPosition);
                } else {
                    areaBeen.setSelect(true);
                    groupViewHolder.imageViewSelect.setSelected(true);
                }
                notifyDataSetChanged();
                //     onItemClickListener.onItemGroupClick(groupPosition);
            }
            //        }
        });
        groupViewHolder.textViewContent.setText(areaBeanList.get(groupPosition).getName());
        return convertView;
    }

    private void setSelect(int index) {
        if (getGroupCount() > index) {
            for (int i = 0; i < areaBeanList.size(); i++) {
                if (i == index) {
                    areaBeanList.get(i).setSelect(true);
                } else {
                    areaBeanList.get(i).setSelect(false);
                }
            }
        }
        notifyDataSetChanged();
    }


    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final GroupViewHolder childViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_area, parent, false);
            childViewHolder = new GroupViewHolder();
            childViewHolder.textViewContent = (TextView) convertView.findViewById(R.id.tv_content);
            childViewHolder.imageViewSelect = (ImageView) convertView.findViewById(R.id.iv_select);
            childViewHolder.ll_area = (RelativeLayout) convertView.findViewById(R.id.ll_area);

            //   groupViewHolder.iv_line =convertView.findViewById(R.id.v_line);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (GroupViewHolder) convertView.getTag();
        }
        final AreaBean customerAreachildbean = areaBeanList.get(groupPosition).getChildren().get(childPosition);
        childViewHolder.imageViewSelect.setVisibility(View.GONE);
        childViewHolder.ll_area.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.selector_item_select_city));
        childViewHolder.textViewContent.setText(customerAreachildbean.getName());
        if (customerAreachildbean.isSelect()) {
            childViewHolder.imageViewSelect.setSelected(true);
        } else {
            childViewHolder.imageViewSelect.setSelected(false);
        }
        childViewHolder.imageViewSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //    if (onItemClickListener != null) {
                if (customerAreachildbean.isSelect()) {
                    customerAreachildbean.setSelect(false);
                    childViewHolder.imageViewSelect.setSelected(false);
                    //   setSelect(groupPosition,childPosition);
                } else {
                    customerAreachildbean.setSelect(true);
                    childViewHolder.imageViewSelect.setSelected(true);
                }
                notifyDataSetChanged();
                //    onItemClickListener.onItemClick(groupPosition, childPosition);
                //        }
                if (childPosition == 0) {
                    // setSelect(groupPosition,childPosition);
//                    if (onItemClickListener != null) {
//                        onItemClickListener.onItemClick(groupPosition,childPosition);
//                    }
                }
            }
        });
        return convertView;
    }

    public void setSelect(int groupPosition, int index) {
        if (getChildrenCount(groupPosition) > index) {
            for (int i = 0; i < areaBeanList.get(groupPosition).getChildren().size(); i++) {
                if (i == index) {
                    areaBeanList.get(groupPosition).getChildren().get(i).setSelect(true);
                } else {
                    areaBeanList.get(groupPosition).getChildren().get(i).setSelect(false);
                }
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private class GroupViewHolder {
        TextView textViewContent;
        ImageView imageViewSelect;
        RelativeLayout ll_area;
        ImageView iv_jiantou;
    }
}
